<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="执行人" prop="userId">
        <el-select
          v-model="queryParams.userId"
          placeholder="请选择执行人"
          style="width: 100%"
        >
          <el-option
            v-for="item in UserList"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['archive:joblist:add']"
          >新增</el-button
        >
      </el-col>
       <!--  <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['archive:joblist:edit']"
        >重置任务</el-button>
      </el-col>
    <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['archive:joblist:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['archive:joblist:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="joblistList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键ID" align="center" prop="id" /> -->
      <el-table-column label="执行人ID" align="center" prop="userId" />
      <el-table-column label="任务详情" align="center" prop="jobDetailStr" />
      <el-table-column label="通知单号" align="center" prop="noticeNo" />
      <el-table-column label="任务类型" align="center" prop="jobType" />
      <el-table-column label="任务代号" align="center" prop="codeName" />
      <el-table-column label="任务编号" align="center" prop="jobCode" />
      <el-table-column label="份数" align="center" prop="serial" />
      <el-table-column label="份数编号" align="center" prop="serialNo" />
      <el-table-column label="载体信息" align="center" prop="carrierInfo" />
      <el-table-column label="用途" align="center" prop="usageInfo" />

      <el-table-column label="任务状态" align="center" prop="jobStatus" >
      <template slot-scope="scope">
        <!-- <p>圣道行</p> -->
        <span v-if="scope.row.jobStatus=='0'">未执行</span>
        <span v-if="scope.row.jobStatus=='1'">正在执行</span>
        <span v-if="scope.row.jobStatus=='2'">已完成</span>
      </template>
    </el-table-column>
      
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
          v-if="scope.row.jobStatus=='1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['archive:joblist:edit']"
          >重置任务</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['archive:joblist:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改任务清单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="执行人" prop="userId">
          <el-select
            v-model="form.userId"
            placeholder="请选择执行人"
            style="width: 100%"
          >
            <el-option
              v-for="item in UserList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="任务详情" prop="jobDetail">
          <div v-for="(item, index) in jobList" :key="index">
            <el-row style="margin-top: 10px; text-align: left">
              <el-col :span="5" style="margin-right:10px">
                <el-select
                  v-model="item.wareHouseId"
                  style="width: 100%"
                  @change="chageselectHouse(item, index)"
                  placeholder="请选择库房"
                >
                  <el-option
                    v-for="item in wareHouseoptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-col>
              <el-col :span="6" style="margin-right:10px">
                <el-select
                  v-model="item.cabinetId"
                  placeholder="请选择柜体"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in item.selectcabinetoptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-col>
              <el-col :span="6" style="margin-right:10px">
                <el-select
                  v-model="item.outboundFlag"
                  placeholder="请选择出入库"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in outboundFlagoptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-col>
              <el-col :span="3" style="margin-right:-20px">
                <el-button @click="removeInput(index)">删除</el-button>
              </el-col>
              <el-col :span="3">
                <el-button @click="addInput">添加</el-button>
              </el-col>
            </el-row>
          </div>
        </el-form-item>
        <el-form-item label="通知单号" prop="noticeNo">
          <el-input v-model="form.noticeNo" placeholder="请输入通知单号" />
        </el-form-item>
        <el-form-item label="任务类型" prop="jobType">
          <el-input v-model="form.jobType" placeholder="请输入任务类型" />
        </el-form-item>
        <el-form-item label="任务代号" prop="codeName">
          <el-input v-model="form.codeName" placeholder="请输入任务代号" />
        </el-form-item>
        <el-form-item label="任务编号" prop="jobCode">
          <el-input v-model="form.jobCode" placeholder="请输入任务编号" />
        </el-form-item>
        <el-form-item label="份数" prop="serial">
          <el-input v-model="form.serial" placeholder="请输入份数" />
        </el-form-item>
        <el-form-item label="份数编号" prop="serialNo">
          <el-input v-model="form.serialNo" placeholder="请输入份数编号" />
        </el-form-item>
        <el-form-item label="载体信息" prop="carrierInfo">
          <el-input v-model="form.carrierInfo" placeholder="请输入载体信息" />
        </el-form-item>
        <el-form-item label="用途" prop="usageInfo">
          <el-input v-model="form.usageInfo" placeholder="请输入用途" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listJoblist,
  getJoblist,
  delJoblist,
  addJoblist,
  updateJoblist,
  getAllOperationUser,
  resetStatus
} from "@/api/archive/joblist";
import { listwareHouse } from "@/api/archive/house";
import { cabinetlist } from "@/api/archive/cabinet";
export default {
  name: "Joblist",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务清单表格数据
      joblistList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //操作人
      UserList: [],
      jobList: [
        {
          wareHouseId: "",
          cabinetId: "",
          outboundFlag: "",
          selectcabinetoptions: [],
        },
      ],
      outboundFlagoptions: [
        {
          name: "出库",
          id: "0",
        },
        {
          name: "入库",
          id: "1",
        },
      ],
      cabinetoptions: [],
      selectcabinetoptions: [],
      queryparmcabinet: {
        wareHouseId: "",
      },
      queryparmselectcabinet: {
        wareHouseId: "",
      },
      wareHouseoptions: [],
    };
  },
  created() {
    this.getList();
    this.getOperationUser();
    this.wareHouse();
  },
  methods: {
    /** 查询任务清单列表 */
    getList() {
      this.loading = true;
      listJoblist(this.queryParams).then((response) => {
        this.joblistList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //选择库房
    wareHouse() {
      listwareHouse().then((response) => {
        this.wareHouseoptions = response.data;
      });
    },
    //选择柜子
    chageselectHouse(e, index) {
      console.log(e);
      this.queryparmselectcabinet.wareHouseId = e.wareHouseId;
      cabinetlist(this.queryparmselectcabinet).then((response) => {
        for (var i in response.data) {
          this.jobList[index].selectcabinetoptions.push({
            name:
              "柜子编号:" +
              response.data[i].code +
              "--" +
              "格子编号:" +
              response.data[i].boxNo,
            id: response.data[i].id,
          });
        }
      });
    },
    //操作人
    getOperationUser() {
      getAllOperationUser().then((response) => {
        this.UserList = response.data;
      });
    },
    //添加删除属性
    addInput() {
      this.jobList.push({
        wareHouseId: "",
        cabinetId: "",
        outboundFlag: "",
        selectcabinetoptions: [],
      });
    },
    removeInput(index) {
      if (this.jobList.length > 1) {
        this.jobList.splice(index, 1);
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        jobDetail: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      (this.jobList = [
        {
          wareHouseId: "",
          cabinetId: "",
          outboundFlag: "",
          selectcabinetoptions: [],
        },
      ]),
        (this.open = true);
      this.title = "添加任务清单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const id = row.id || this.ids;
      resetStatus(id).then((response) => {
        this.$modal.msgSuccess("重置成功");
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        this.form.jobDetail = "";
        if (valid) {
          if (this.form.id != null) {
            updateJoblist(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            for (var i in this.jobList) {
              if (
                this.jobList[i].wareHouseId == "" ||
                this.jobList[i].cabinetId == "" ||
                this.jobList[i].outboundFlag == ""
              ) {
                this.$modal.msgError("请填写任务信息");
              } else {
                this.form.jobDetail +=
                  this.jobList[i].cabinetId +
                  "," +
                  this.jobList[i].outboundFlag +
                  ";";
              }
            }
            addJoblist(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除任务清单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delJoblist(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "archive/joblist/export",
        {
          ...this.queryParams,
        },
        `joblist_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
